<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\file\FileInput;
use yii\helpers\Url;
use dosamigos\ckeditor\CKEditor;

/* @var $this yii\web\View */
/* @var $model common\models\Goods */
/* @var $form yii\widgets\ActiveForm */
/* @var $upload \common\models\UploadForm */

?>

<div class="box-body">
    
    <?php $form = ActiveForm::begin([
        'options'=>['class'=>'form-horizontal'],
        'fieldConfig'=>[
            'template'=>"{label}\n<div class=\"col-sm-10\">{input}\n{hint}\n{error}</div>",
            'labelOptions'=>['class'=>'col-sm-2 control-label'],
        ],
    ]); ?>
    
    <?= $form->field($model, 'goods_name', ['validateOnChange' => true])->textInput(['maxlength' => true]) ?>
    
    <?= $form->field($model, 'goods_price')->textInput(['maxlength' => true]) ?>
    
    <?= $form->field($model, 'goods_thumbnail', ['inputOptions' => ['id' => 'goods-thumbnail'],])->hint(FileInput::widget([
        'model' => $upload,
        'attribute' => 'imageFile',
        'options' => ['multiple' => true],
        'pluginOptions' => [
            'previewFileType' => 'image',
            'initialPreview' => $model->file->file_path ? [$model->file->file_path] : [],
            // 是否展示预览图
            'initialPreviewAsData' => true,
            'showUpload' => true,
            'maxFileCount' => 1,
            'uploadUrl' => Url::toRoute(['upload/upload']),
            'uploadAsync' => true,
            'deleteUrl' => Url::toRoute(['upload/delete']),
            'deleteExtraData' => [
                'file_id' => $model->goods_thumbnail
            ],
            'uploadExtraData' => [
                'type' => \common\models\UploadForm::GOODS_TYPE,
            ],
            'maxFileCount' => 1,
            'showRemove' => false,
            'dropZoneEnabled' => false,
        ],
        'pluginEvents' => [
            // 上传成功后的回调方法，需要的可查看data后再做具体操作，一般不需要设置
            "fileuploaded" => "function (event, data, id, index) {
                if( data.response.data.id > 0 ){
                    $('#goods-thumbnail').val(data.response.data.id);
                    $('.field-goods-thumbnail').removeClass('has-error').addClass('has-success').find('.help-block').html('')
                }
            }",
            "filedeleted" => "function (event, data, id, index) {
                    $('#goods-thumbnail').val('');
            }",
        ],
    ]))->hiddenInput()->error(['']) ?>
    
    <?= $form->field($model, 'goods_desc')->widget(CKEditor::className(), [
        'options' => ['rows' => 6],
        'preset' => 'full'
    ]) ?>
    

    <div class="box-footer">
        <a href="javascript:history.back();" class="btn btn-default">取消</a>
        <?= Html::submitButton($model->isNewRecord ? '创建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success pull-right' : 'btn btn-primary pull-right']) ?>
    </div>
    
    <?php ActiveForm::end(); ?>

</div>

